package com.lsw.jgyf.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lsw.Base.BaseHolder;
import com.lsw.Base.BaseSimpleAdapter;
import com.lsw.jgyf.R;
import com.lsw.jgyf.model.HomeFragment1Model;
import com.lsw.jgyf.model.PhoneModel;
import com.lsw.jgyf.util.GlideUtils;
import com.lsw.util.DimenUtils;

/**
 * Created by lsw on 2018/7/8.
 */

public class HomeFragment1Adapter extends BaseSimpleAdapter<HomeFragment1Model.PhoneListEntity.DataEntity> {
    public HomeFragment1Adapter(Context context) {
        super(context);
        initImage();
    }

    private int width_Img, height_Img;

    /**
     * 动态计算图片宽高，左右边距为5，图片左右边距为5
     */
    private void initImage() {
        width_Img = (DimenUtils.getScreenSize((Activity) context)[0] - DimenUtils.dip2px(context, 32)) / 2;
        height_Img = (int) (width_Img * 115f / 171f);
//        height_Img = width_Img;
    }

    @Override
    protected BaseHolder<HomeFragment1Model.PhoneListEntity.DataEntity> getHolder() {
        return new BaseHolder<HomeFragment1Model.PhoneListEntity.DataEntity>() {
            //合约机
            ImageView imageview;
            ImageView image;
            TextView tv_price;
            TextView tv_name;

            @Override
            public void bindViews(View parentView) {
                imageview = parentView.findViewById(R.id.imageview);
                image = parentView.findViewById(R.id.image);
                tv_price = parentView.findViewById(R.id.tv_price);
                tv_name = parentView.findViewById(R.id.tv_name);
                ViewGroup.LayoutParams params = image.getLayoutParams();
                if (params != null) {
                    params.width = width_Img;
                    params.height = height_Img;
                }
            }

            @Override
            public void bindData(HomeFragment1Model.PhoneListEntity.DataEntity item, int position) {
                GlideUtils.loadImageView(context, item.getImg_url(), image);
                tv_name.setText(item.getPhoneBrand().getName() + item.getName());
                tv_price.setText(item.getPhoneSku().getPrice());
                if (item.getIs_contract() == 1) {
                    imageview.setVisibility(View.VISIBLE);
                } else {
                    imageview.setVisibility(View.INVISIBLE);
                }
            }
        };
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.item_gridview_home1;
    }
}
